package edu.zut.cs.software.cms.module.all.clazz.service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import edu.zut.cs.software.cms.base.service.GenericGenerator;
import edu.zut.cs.software.cms.module.all.clazz.domain.Clazz;

/**
 * Generator for Clazz Entity
 * 
 * @author 42276
 *
 */
public class ClazzEntityGenerator extends GenericGenerator {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LogManager.getLogger(ClazzEntityGenerator.class.getName());

	/**
	 * Logger for this class
	 */

	List<Clazz> clazzList;

	ClazzManager clazzManager;

	@Autowired
	public void setClazzManager(ClazzManager clazzManager) {
		this.clazzManager = clazzManager;
	}

	@Before
	public void setUp() throws Exception {
		this.clazzList = new ArrayList<Clazz>();
		InputStream input = ClazzManagerTest.class.getResourceAsStream("/javaee_clazz_software.xlsx");
		@SuppressWarnings("resource")
		XSSFWorkbook wb = new XSSFWorkbook(input);
		XSSFSheet sheet = wb.getSheetAt(0);
		DataFormatter dataFormatter = new DataFormatter();
		// int rows =sh.getLastRowNum()+1-sh.getFirstRowNum();
		for (int i = 1; i <= sheet.getLastRowNum(); i++) {
			Row row = sheet.getRow(i);
			Clazz c = new Clazz();

			for (int j = 0; j < row.getLastCellNum(); j++) {
				Cell cell = row.getCell(j);
				if (cell != null) {
					String value = dataFormatter.formatCellValue(cell);
					if (j == 1)
						c.setDepartment(value);
					if (j == 2)
						c.setHead_teacher(value);
					if (j == 3)
						c.setMajor(value);
					if (j == 4)
						c.setName(value);
					if (j == 5)
						c.setNumber(value);
				}
			}
			this.clazzList.add(c);
		}
	}

	@Test
	public void test() {
		//this.clazzManager.deleteAll();
		this.clazzManager.save(this.clazzList);
		Iterable<Clazz> result = this.clazzManager.findAll();
		if (logger.isInfoEnabled()) {
			logger.info("test() - List<Clazz> result=" + result); //$NON-NLS-1$
		}

	}
}
